package com.silvtar.sync.model;

import java.util.GregorianCalendar;
import java.util.Timer;
import java.util.TimerTask;

import com.silvtar.io.StdOut;
import com.silvtar.sync.controller.SilverySyncInit;
import com.silvtar.util.FolderMonitor;
import com.silvtar.util.SSPlatform;
import com.sun.jna.Platform;

public class SSTrigger{
    
    SSTrigger ssTrigger = this;
    SilverySyncInit ssInit;
    Timer timer;
    FolderMonitor folderMonitor;
    boolean syncRuning = false;
    
    class Task extends TimerTask {

        @Override
        public void run() {
            StdOut.printLog("开始同步");
            while ( !syncRuning) {
                syncRuning = true;
                ssInit.startSync(ssTrigger);
                syncRuning = false;
                break;
            }
        }
    }
    
    public SSTrigger(SilverySyncInit ssInit) {
        this.ssInit = ssInit;
        timer = new Timer(true);
    }
    
    public void setInterval(long sec) {
        timer.schedule(new Task(), 1000*sec, 1000*sec);
    }

    public void setSyncMoment() {
        timer.schedule(new Task(), new GregorianCalendar().getTime());
    }
    
    public void setMonitor(String path, long sec) {
        if (SSPlatform.platform == SSPlatform.MAC) {
            folderMonitor = new FolderMonitor(this, path, sec);
            folderMonitor.start();
        }
    }
    
    public void monitorRun() {
        StdOut.printLog("开始同步");
        while ( !syncRuning) {
            syncRuning = true;
            ssInit.startSync(ssTrigger);
            syncRuning = false;
            break;
        }
    }
    
}
